/*
 * MedicineSelectPopup.java
 *
 * Created on __DATE__, __TIME__
 */

package rj.ui.utils;

import java.awt.Component;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.ListModel;
import javax.swing.event.ListDataListener;

import rj.db.suggest.MedicineSuggest;
import rj.db.utils.ConnectionBean;
import rj.ui.MithyaHome;

/**
 *
 * @author  __USER__
 */
public class MedicineSelectPopup extends javax.swing.JDialog
{
	
	/** Creates new form MedicineSelectPopup */
	public MedicineSelectPopup(JFrame parent, boolean modal)
	{
		super(parent, modal);
		initComponents();
		this.setVisible(true);
		selectMedicineBtn.setEnabled(false);
		medicineList.setEnabled(false);
	}
	
	public String seletedValue;
	
	public MedicineSelectPopup(boolean modal)
	{
		this(MithyaHome.mainFrame, modal);
	}
	
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents()
	{
		
		jPanel1 = new javax.swing.JPanel();
		medicineLbl = new javax.swing.JLabel();
		medicineNameTxt = new javax.swing.JTextField();
		jScrollPane1 = new javax.swing.JScrollPane();
		medicineList = new javax.swing.JList();
		selectMedicineBtn = new javax.swing.JButton();
		
		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		
		medicineLbl.setText("Medicine Name");
		
		medicineNameTxt.addKeyListener(new java.awt.event.KeyAdapter()
		{
			public void keyReleased(java.awt.event.KeyEvent evt)
			{
				medicineNameTxtKeyReleased(evt);
			}
		});
		
		medicineList.setModel(new javax.swing.AbstractListModel()
		{
			String[] strings = { "Type in box above" };
			
			public int getSize()
			{
				return strings.length;
			}
			
			public Object getElementAt(int i)
			{
				return strings[i];
			}
		});
		medicineList.setEnabled(false);
		medicineList.addFocusListener(new java.awt.event.FocusAdapter()
		{
			public void focusGained(java.awt.event.FocusEvent evt)
			{
				medicineListFocusGained(evt);
			}
		});
		medicineList.addKeyListener(new java.awt.event.KeyAdapter()
		{
			public void keyPressed(java.awt.event.KeyEvent evt)
			{
				medicineListKeyPressed(evt);
			}
		});
		jScrollPane1.setViewportView(medicineList);
		
		selectMedicineBtn.setText("Select Medicine");
		selectMedicineBtn.setEnabled(false);
		selectMedicineBtn.addActionListener(new java.awt.event.ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent evt)
			{
				selectMedicineBtnActionPerformed(evt);
			}
		});
		
		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(
						jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addComponent(jScrollPane1,
								javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 332, Short.MAX_VALUE).addGroup(
								javax.swing.GroupLayout.Alignment.LEADING,
								jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addComponent(selectMedicineBtn).addGroup(
										javax.swing.GroupLayout.Alignment.LEADING,
										jPanel1Layout.createSequentialGroup().addComponent(medicineLbl).addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(medicineNameTxt,
												javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)))).addContainerGap()));
		jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(
						jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(medicineLbl).addComponent(
								medicineNameTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
								javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(
						jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE).addPreferredGap(
						javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(selectMedicineBtn).addContainerGap()));
		
		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jPanel1,
				javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jPanel1,
				javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
		
		pack();
	}// </editor-fold>
	//GEN-END:initComponents
	
	private void medicineListKeyPressed(java.awt.event.KeyEvent evt)
	{
		if(evt.getKeyCode() == 10){
			this.dispose();
			seletedValue = medicineList.getSelectedValue().toString();
		}
		
	}
	
	private void selectMedicineBtnActionPerformed(java.awt.event.ActionEvent evt)
	{
		this.dispose();
		seletedValue = medicineList.getSelectedValue().toString();
		System.out.println(seletedValue);
	}
	
	private void medicineListFocusGained(java.awt.event.FocusEvent evt){
		medicineList.setSelectedIndex(0);
		selectMedicineBtn.setEnabled(true);
	}
	
	ArrayList<String> medicineArrayList = new ArrayList<String>();
	private void medicineNameTxtKeyReleased(java.awt.event.KeyEvent evt){
		medicineList.setEnabled(true);
		try{
			medicineArrayList = MedicineSuggest.getMedicineByStartName(medicineNameTxt.getText().trim());
		}
		catch (SQLException e){
			e.printStackTrace();
		}
		medicineList.setModel(new DefaultListModel(){
			@Override
			public int getSize(){
				return medicineArrayList.size();
			}
			
			@Override
			public Object getElementAt(int index){
				return medicineArrayList.get(index);
			}
		});
	}
	
	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[])
	{
		java.awt.EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				MedicineSelectPopup dialog = new MedicineSelectPopup(new javax.swing.JFrame(), true);
				dialog.addWindowListener(new java.awt.event.WindowAdapter()
				{
					public void windowClosing(java.awt.event.WindowEvent e)
					{
						System.exit(0);
					}
				});
				dialog.setVisible(true);
			}
		});
	}
	
	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JPanel jPanel1;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JLabel medicineLbl;
	private javax.swing.JList medicineList;
	private javax.swing.JTextField medicineNameTxt;
	private javax.swing.JButton selectMedicineBtn;
	// End of variables declaration//GEN-END:variables
	
}
